﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Spring.Context;
using NHibernate;
using Spring.Context.Support;
using NHibernate.Context;
namespace Wekit.Test
{
    [TestClass]
    public abstract class AbstractTestBase
    {
        protected IApplicationContext ApplicationContext;

        private ISessionFactory sessionFactory;
        private ISession session;

       
        public void init()
        {
            if (ApplicationContext == null)
            {
                ApplicationContext = ContextRegistry.GetContext();
            }
            if(sessionFactory==null)
                sessionFactory = getObject("NHibernateSessionFactory") as ISessionFactory;
        }

        public object getObject(string name)
        {
            return ApplicationContext.GetObject(name);
        }

        private ISession openSession()
        {
           session= sessionFactory.OpenSession() ;
           return session;
        }

        private void closeSession()
        {
            if (session != null && session.IsOpen)
            {      
                session.Close();
            }
        }




        [TestInitialize()]
        public void testMethodStart()
        {
            init();
            openSession();
        }
       
        [TestCleanup()]
        public void testMethodEnd()
        {
            closeSession();
        }

        protected ISession getSession()
        {
            return session;
        }

    }
}
